package com.lanou.pianke.article.service;

import com.github.pagehelper.PageHelper;
import com.lanou.pianke.article.entity.Article;
import com.lanou.pianke.article.entity.ArticleComment;
import com.lanou.pianke.article.entity.ArticleInfo;
import com.lanou.pianke.article.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ArticleService {

   @Autowired
   private ArticleMapper articleMapper;


   @Autowired
   private  RedisTemplate redisTemplate;

   public List<String> selectArticleId(){
       return articleMapper.selectArticleId();
   }

   public Integer selectCommentNumber(String contentId){
       return articleMapper.selectCommentNumber(contentId);
   }

   public void updateArticleComment(String contentId,Integer commentNumbers){
       articleMapper.updateArticleComment(contentId,commentNumbers);
   }

    public List<Article> selectArticle(Integer categoryId) {
      return articleMapper.selectArticleByAcid(categoryId);
    }

    public ArticleInfo selectArticleInfo(String contentId) {
        return articleMapper.selectArticleInfo(contentId);
    }

    public Integer selectReplyNumber(String contentId) {
        return articleMapper.selectReplyNumber(contentId);
    }

    public List<String> selectCommentId(String contentId) {
        return articleMapper.selectCommentId(contentId);
    }

    public List<ArticleComment> selectCommentInfo(String contentId) {
        return articleMapper.selectCommentInfo(contentId);
    }

    public void insertArticle(Article article) {
        articleMapper.insertArticle(article);
        articleMapper.insertArticleContent(article);
    }

    public void insertArticleComment(ArticleComment articleComment) {
       articleMapper.insertArticleComment(articleComment);
    }

    public List<Article> showAll() {
       return articleMapper.showAll();
    }

    public List<Article> findByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return articleMapper.showAll();
    }

    public void addArtcileRedis() {
        List<Article> articles = articleMapper.showAll();
        for (int i = 0; i < articles.size(); i++) {
            Map<String,Object> hotArtical = new HashMap<>();
            Article article = articles.get(i);
            Integer comments = article.getComments();
            Integer likes = article.getLikes();
            Integer readCount = article.getReadCount();
            int hots = readCount*5+likes*10+comments*20;
            if (hots>=10000){
                hotArtical.put("hotArticle",article);
                hotArtical.put("hot",hots);
                ZSetOperations opsForZSet = redisTemplate.opsForZSet();
                opsForZSet.add("hotArticles",hotArtical,hots);
            }
        }
    }

    public List<Article> showHotArticle() {

        Set hotArticles = redisTemplate.opsForZSet().reverseRange("hotArticles",0,2);
        List list = new ArrayList<>();
        for (Object hotArticle : hotArticles) {
             list.add(hotArticle);
        }
        return list;
    }
}
